<template>
  <div>
    <div class="topBtn">
      <div class="sizeBig"><span>订单管理</span></div>
      <div>
<!--        <el-button   @click="resetData"  style="height:39px" type="primary">查询</el-button>-->
        <el-button @click="edit" type="primary">
          <i class="el-icon-edit"></i>编辑
        </el-button>
        <el-button @click="orderQuery" type="primary">
          <i class="icon-log"></i>订单日志
        </el-button>
        <el-button @click="urgent" type="primary">
          <i class="icon-urgent"></i>加急
        </el-button>
      </div>
    </div>
    <div class="message">
      <p class="character">1.生产计划，就是告诉产线接下来做什么，本功能主要是解决订单交付延迟问题，对从商城同步过来的订单，系统按订单优先级+剩余发货时长排序。</p>
      <p class="character">2.生产计划员，对即将超时订单，在保证产能足以覆盖的前题下，对订单进行勾选转换成生产计划。<span class="skip">查看帮助文档</span></p>
    </div>
    <div class="inputcontent">
      <el-input @keydown.native.enter="resetData" class="inSearch" placeholder="请输入内容" v-model="search"></el-input>
      <el-select @change="resetData" class="select" placeholder="订单状态" v-model="value">
        <el-option
          :key="index"
          :label="item.label"
          :value="item.value"
          v-for="(item,index) in options">
        </el-option>
      </el-select>
      <el-date-picker
        :picker-options="pickerOptions"
        @change="resetData"
        end-placeholder="结束日期"
        start-placeholder="开始日期"
        style="width:360px"
        type="daterange"
        unlink-panels
        v-model="value2"
        value-format="yyyy-MM-dd">
      </el-date-picker>
      <div @click="resetData" class="iconSearch"></div>
    </div>

    <div class="tableContent">
      <Table :init="init" :reset="reset" @changeData="changedata"></Table>
    </div>

    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-size="size"
        :page-sizes="[10,20,50,100]"
        :total="TotalNum"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>

    <!--订单编辑弹出框 -->
    <el-dialog :visible.sync="dialogEditVisible" title="订单编辑">
      <el-form :model="editFormData" ref="editFormData">
        <el-form-item label="配送方式:" label-width="130px">
          <ASelect :options="companyOptions" :labelAttrName="'Name'" :valAttrName="'ShippingCompanyID'" v-model="editFormData.CompanyId"></ASelect>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 订单日志弹出框 -->
    <el-dialog :close-on-click-modal='false' :visible.sync="dialogTableVisible" center title="订单日志">
      <el-table :data="getData" border>
        <el-table-column align="center" label="编号" property="LogId" width="80"></el-table-column>
        <el-table-column align="center" label="订单" property="Name" width="80"></el-table-column>
        <el-table-column align="center" label="操作人" property="OperateUseBy"></el-table-column>
        <el-table-column align="center" label="操作类型" property="OperateTypeFormat"></el-table-column>
        <el-table-column align="center" label="操作时间" property="OperateDate"></el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-size="pageSize"
          :page-sizes="[10, 20]"
          :total="ordertotal"
          @current-change="handleCurrent"
          @size-change="handleSize"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Table from './components/projectTable'
  import operation from '@/mixins/operation'
  import { GetAllShippingCompanys, getData, getOrder, ModifyOrderShippingCompany, urgentOrderBag } from '@/api/project'
  import ASelect from '@/components/ATheFridayClub/ASelect'
  import { ERR_OK } from '@/utils/commons'

  export default {
    name: 'project',
    mixins: [operation],
    data() {
      return {
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        value2: [],
        options: [{
          value: -1,
          label: '所有'
        }, {
          value: 0,
          label: '暂停'
        }, {
          value: 1,
          label: '已下单'
        }, {
          value: 2,
          label: '正在生产'
        },
          // {
          //   value: 3,
          //   label: '完成'
          // },
          {
            value: 4,
            label: '已发货'
          }],
        dialogTableVisible: false,   //订单日志查询
        dialogEditVisible:false,
        value: '',
        module: 'project',  //api发送请求
        search: '', //搜索内容
        getData: [],   //获取所有订单日志
        ordertotal: 0,
        addlist: [],//勾选的数据
        checkIds: [],//勾选的id数组
        currentPage: 1, //初始页
        size: 10, //每页显示多少条
        // val: 1,//页码
        TotalNum: 0,//数据总条数
        pageSize: 10,
        companyOptions:[],
        editFormData:{
          OrderId:'',
          CompanyId:'',
        }
      }
    },
    components: {
      Table,ASelect
    },
    created() {
      // this.init({ keyName: this.search, status: -1, page: 1, pageSize: 5 })
      this.init()
    },
    methods: {
      async edit() {
        if(this.editFormData.OrderId === ''){
          this.$message.warning('请先选择需要编辑的订单！')
        }else {
          this.dialogEditVisible = true
          const { Result,Code }= await GetAllShippingCompanys()
          if(Code === ERR_OK){
            this.companyOptions = Result
          }
        }
      },
      async submitEdit() {
          console.log(this.editFormData,196)
          const {Result,Code} = await ModifyOrderShippingCompany({
            ...this.editFormData
          })
          if (Code === ERR_OK){
            this.$message.success('编辑成功!')
            this.dialogEditVisible = false
            this.init();
          }
      },
      // 自动搜索的方法
      async fetchData(val) {
        this.init({ keyName: this.search })
      },
      // 订单列表数据分页
      handleSizeChange(size) {
        this.size = size
        this.init()
      },
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage
        this.init()
      },

      resetData() {
        this.currentPage = 1
        this.init()
      },
      // 请求订单列表数据
      init() {
        if (!this.value2) {
          getData({
            keyName: this.search,
            status: parseInt(this.value),
            page: this.currentPage,
            pageSize: this.size
          }).then(res => {
            if (parseInt(res.Code) === 1) {
              this.reset = res.Result.Items
              this.TotalNum = res.Result.TotalNum
            }
          })
        } else {
          getData({
            keyName: this.search,
            status: parseInt(this.value),
            page: this.currentPage,
            pageSize: this.size,
            startTime: this.value2[0],
            endTime: this.value2[1]
          }).then(res => {
            if (parseInt(res.Code) === 1) {
              this.reset = res.Result.Items
              this.TotalNum = res.Result.TotalNum
            }
          })
        }
      },

      // 订单日志分页
      handleSize(pageSize) {
        this.pageSize = pageSize
        getOrder({ businessType: 12, page: 1, pageSize: this.pageSize }).then((res) => {
          this.getData = res.Result.Items
        }).then((error) => {

        })
      },
      handleCurrent(val) {
        getOrder({ businessType: 12, page: val, pageSize: this.pageSize }).then((res) => {
          this.getData = res.Result.Items
        }).then((error) => {

        })
      },
      // 订单日志
      orderQuery() {

        this.dialogTableVisible = true
        getOrder({ businessType: 12, page: 1, pageSize: 10 }).then((res) => {
          this.getData = res.Result.Items
          this.ordertotal = res.Result.TotalNum
        }).then((error) => {
          console.log(error)
        })
      },

      async changedata(data) {
        this.addlist = data
        console.log(this.addlist[0].OrderbagId)
        this.editFormData.OrderId = this.addlist[0].OrderbagId
      },

      // 加急
      urgent() {
        if (this.addlist.length < 1) {
          this.$message('请勾选需要加急的订单')
        } else {
          let listid = ''
          this.addlist.forEach(el => {
            listid += el.OrderbagId + ','
          })
          listid = listid.substr(0, listid.length - 1)
          urgentOrderBag({ strOrderbagId: listid }).then(res => {
            if (parseInt(res.Code) === 1) {
              this.$message({
                type: 'success',
                message: '加急成功'
              })
            }
            this.init()
          })
        }
      }
    }

  }
</script>

<style lang="scss" scoped>
  .topBtn {
    height: 60px;
    width: 98%;
    margin: 0 auto;
    line-height: 60px;
    display: flex;
    justify-content: space-between;

    .icon-log {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin-bottom: 2px;
      background: url("image/dingdanrizhi.png") no-repeat;
      vertical-align: middle;
    }

    .icon-urgent {
      display: inline-block;
      width: 14px;
      height: 14px;
      margin-bottom: 2px;
      background: url("image/jiaji.png") no-repeat;
      vertical-align: middle;
    }
  }

  .message {
    width: 98%;
    margin: 0 auto;
    height: 100px;
    background: rgba(255, 255, 255, 1);
    border: 1px solid rgba(238, 238, 238, 1);
    font-size: 14px;

    .character {
      margin: 20px 0 21px 20px;
      color: #999999;

      .skip {
        color: #0095FF;
        margin-left: 10px;
        cursor: pointer;
      }
    }
  }

  .inputcontent {
    width: 98%;
    margin: 0 auto;
    height: 80px;
    position: relative;
    padding-top: 21px;
    box-sizing: border-box;

    .iconSearch {
      position: absolute;
      left: 277px;
      top: 30px;
      width: 20px;
      height: 20px;
      background-image: url('./image/搜索.png');
      background-repeat: no-repeat;
      cursor: pointer;
    }

    .inSearch {
      width: 308px;
      height: 40px;
    }

    .select {
      margin-right: 20px;
      margin-left: 20px;
    }
  }

  .tableContent {
    width: 98%;
    height: 100%;
    margin: 0 auto;
  }

  .block {
    text-align: right;
    margin: 28px 30px 50px 0;
  }

</style>
